<!--
 * @Author: 颜礼田 2564217790@qq.com
 * @Date: 2024-08-26 15:21:26
 * @LastEditors: MrDotYan 2564217790@qq.com
 * @LastEditTime: 2024-09-21 13:37:19
 * @FilePath: /turo-repo-enterprise/apps/fuck-enterprise-admin/src/views/system/permission/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="table-box">
    <ProTable ref="proTableRef" :columns="columns" :request-api="fetchPermissionList">
      <template #tableHeader>
        <el-button type="primary" v-auth="'System.Permission.AddPermission'" @click="open">新增</el-button>
      </template>
    </ProTable>
    <Edit ref="editRef" @success="refresh"></Edit>
  </div>
</template>

<script lang="tsx" setup>
import ProTable from "@/components/ProTable/index.vue";
import { ColumnProps, ProTableInstance } from "@/components/ProTable/interface";
import { fetchPermissionList, removePermission } from "@/api/modules/permission";

import Edit from "./components/edit.vue";
import { ref } from "vue";
import { ElMessageBox, ElNotification } from "element-plus";
import { ResultEnum } from "@/enums/httpEnum";

const editRef = ref();
const proTableRef = ref<ProTableInstance>();
function open(row) {
  editRef.value?.open(row);
}

function refresh() {
  proTableRef.value?.getTableList();
}

function edit(row) {
  open(row);
}

function remove(row) {
  ElMessageBox.confirm("提示", { type: "error", message: "是否删除当前数据？" })
    .then(() => {
      if (row?.id) {
        removePermission({ id: row.id }).then(r => {
          const { code, message } = r;
          if (code == ResultEnum.SUCCESS) {
            ElNotification.success("删除成功！");
            refresh();
          } else {
            ElNotification.error(message);
          }
        });
      }
    })
    .catch(err => {
      console.error(err);
    });
}

const columns: Array<ColumnProps> = [
  { label: "序号", type: "index", fixed: "left", width: 60 },
  { prop: "permissionCode", label: "权限编码", search: { el: "input" } },
  { prop: "permissionName", label: "权限名称", search: { el: "input" } },
  {
    prop: "action",
    label: "操作",
    fixed: "right",
    render: ({ row }) => {
      return (
        <>
          <el-button type="primary" v-auth="System.Permission.EditPermission" link onClick={() => edit(row)}>
            编辑
          </el-button>
          <el-button type="danger" v-auth="System.Permission.RemovePermission" link onClick={() => remove(row)}>
            删除
          </el-button>
        </>
      );
    }
  }
];
</script>

<style lang="scss" scoped></style>
